package com.hina.rec.endpoint.system;

import cn.dev33.satoken.annotation.SaIgnore;
import com.hina.rec.application.service.system.AuthService;
import com.hina.rec.common.model.Result;
import com.hina.rec.endpoint.req.system.LoginReq;
import com.hina.rec.endpoint.req.system.ResetPasswordReq;
import com.hina.rec.endpoint.res.system.LoginRes;
import com.hina.rec.endpoint.res.system.MenuTreeRes;
import com.hina.rec.endpoint.res.system.UserInfoRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 登录管理接口
 *
 * @author liaosp
 * @date 2024-01-10
 */
@Tag(name = "登录管理接口")
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    @SaIgnore
    @PostMapping("/login")
    @Operation(summary = "登录")
    public Result<LoginRes> login(@RequestBody @Valid LoginReq req) {
        return Result.success(authService.login(req));
    }

    @SaIgnore
    @PostMapping("/logout")
    @Operation(summary = "注销")
    public Result<Boolean> logout() {
        return Result.success(authService.logout());
    }

    @PostMapping("/refresh_token")
    @Operation(summary = "刷新令牌")
    public Result<String> refreshToken() {
        return Result.success(authService.refreshToken());
    }

    @PostMapping("/user/info")
    @Operation(summary = "获取登录用户的基本信息")
    public Result<UserInfoRes> getUserInfo() {
        return Result.success(authService.getUserInfo());
    }

    @PostMapping("/user/menu_tree")
    @Operation(summary = "获取登录用户的菜单树")
    public Result<List<MenuTreeRes>> getUserMenuTree() {
        return Result.success(authService.getUserMenuTree());
    }

    @PostMapping("/reset-password")
    @Operation(summary = "重置密码")
    public Result<Boolean> resetPassword(@RequestBody @Valid ResetPasswordReq req) {
        return Result.success(authService.resetPassword(req));
    }
}